package com.jumi.microservice.material.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jumi.microservice.material.domain.JmMaterialAttribute;
import com.jumi.microservice.material.domain.JmMaterialAttributeValue;
import com.jumi.microservice.material.domain.example.JmMaterialAttributeValueExample;
import com.jumi.microservice.material.mapper.JmMaterialAttributeMapper;
import com.jumi.microservice.material.mapper.JmMaterialAttributeValueMapper;
import com.jumi.microservice.material.service.JmMaterialAttributeValueService;

/**
 * @description: 商品属性值service服务组件
 * @author: ZhangY
 */
@Service
@Transactional
public class JmMaterialAttributeValueServiceImpl implements JmMaterialAttributeValueService
{
	@Autowired
	private JmMaterialAttributeValueMapper jmMaterialAttributeValueMapper;

	@Autowired
	private JmMaterialAttributeMapper jmMaterialAttributeMapper;

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public PageInfo<JmMaterialAttributeValue> findAll(Integer page, Integer size, String attrValueName, Integer attrId, Integer attrValueStatus, Date beginTime, Date endTime)
	{
		if (attrValueName == null || attrValueName.length() <= 0)
		{
			attrValueName = null;
		}
		JmMaterialAttributeValueExample jmMaterialAttributeValueExample = new JmMaterialAttributeValueExample();
		jmMaterialAttributeValueExample.setOrderByClause("update_time DESC");
		JmMaterialAttributeValueExample.Criteria attrValueExampleCriteria = jmMaterialAttributeValueExample.createCriteria();
		if (!StringUtils.isEmpty(attrValueName))
		{
			attrValueExampleCriteria.andAttrValueNameLike("%" + attrValueName + "%");
		}
		attrValueExampleCriteria.andIsDelEqualTo(0);
		attrValueExampleCriteria.andAttrIdEqualTo(attrId);
		attrValueExampleCriteria.andAttrValueStatusEqualTo(attrValueStatus);
		attrValueExampleCriteria.andUpdateTimeGreaterThanOrEqualTo(beginTime);
		attrValueExampleCriteria.andUpdateTimeLessThanOrEqualTo(endTime);
		PageHelper.startPage(page, size);
		List<JmMaterialAttributeValue> attrValues = jmMaterialAttributeValueMapper.selectByExample(jmMaterialAttributeValueExample);
		return new PageInfo<>(attrValues);
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public JmMaterialAttributeValue findById(Integer attrValueId)
	{
		return jmMaterialAttributeValueMapper.selectByPrimaryKey(attrValueId);
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public int add(JmMaterialAttributeValue attrValue)
	{
		return jmMaterialAttributeValueMapper.insertSelective(attrValue);
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public int update(JmMaterialAttributeValue attrValue)
	{
		return jmMaterialAttributeValueMapper.updateByPrimaryKeySelective(attrValue);
	}

	@Override
	public int deleteById(Integer attrValueId, String updateBy)
	{
		//启动状态不能删除
		JmMaterialAttributeValueExample jmMaterialAttributeValueExample = new JmMaterialAttributeValueExample();
		JmMaterialAttributeValueExample.Criteria attrValueExampleCriteria = jmMaterialAttributeValueExample.createCriteria();
		attrValueExampleCriteria.andAttrValueStatusEqualTo(1);
		attrValueExampleCriteria.andAttrValueIdEqualTo(attrValueId);
		List<JmMaterialAttributeValue> attrValues = jmMaterialAttributeValueMapper.selectByExample(jmMaterialAttributeValueExample);
		if (attrValues.size() > 0)
		{
			return 0;
		}

		return jmMaterialAttributeValueMapper.deleteByPrimaryKey(attrValueId, updateBy);
	}

	@Override
	public int setStatus(Integer attrValueId, Integer attrValueStatus, String updateBy)
	{
		JmMaterialAttributeValue attrValue = new JmMaterialAttributeValue();
		attrValue.setUpdateBy(updateBy);

		//禁用
		if (attrValueStatus == 0)
		{
			attrValue.setAttrValueId(attrValueId);
			attrValue.setAttrValueStatus(attrValueStatus);
			return jmMaterialAttributeValueMapper.updateByPrimaryKeySelective(attrValue);
		}
		//启用
		attrValue.setAttrValueId(attrValueId);
		attrValue.setAttrValueStatus(attrValueStatus);
		return jmMaterialAttributeValueMapper.updateByPrimaryKeySelective(attrValue);
	}

	@Override
	public ArrayList<Object> findValuesByAttrIds(String attrIdsString)
	{
		String[] attrIds = attrIdsString.split(",");
		ArrayList<Object> list = new ArrayList<>();
		for (String attrId : attrIds)
		{
			HashMap<String, Object> map = new HashMap<>();
			ArrayList<Object> attrValueIdList = new ArrayList<>();
			ArrayList<Object> attrValueNameList = new ArrayList<>();
			JmMaterialAttributeValueExample attributeValueExample = new JmMaterialAttributeValueExample();
			JmMaterialAttributeValueExample.Criteria attrValueExampleCriteria = attributeValueExample.createCriteria();
			;
			attrValueExampleCriteria.andAttrIdEqualTo(Integer.valueOf(attrId));
			attrValueExampleCriteria.andIsDelEqualTo(0);
			List<JmMaterialAttributeValue> attrValues = jmMaterialAttributeValueMapper.selectByExample(attributeValueExample);
			for (JmMaterialAttributeValue attrValue : attrValues)
			{
				Integer attrValueId = attrValue.getAttrValueId();
				String attrValueName = attrValue.getAttrValueName();
				attrValueIdList.add(attrValueId);
				attrValueNameList.add(attrValueName);
			}
			JmMaterialAttribute attribute = jmMaterialAttributeMapper.selectByPrimaryKey(Integer.valueOf(attrId));
			map.put("attrId", attrId);
			map.put("attrName", attribute.getAttrName());
			map.put("attrValueIds", attrValueIdList);
			map.put("attrValueName", attrValueNameList);
			list.add(map);
		}
		return list;
	}

	@Override
	public int getAttrValueId(String attrId, String attrValueName, String createBy)
	{
		JmMaterialAttributeValue value = new JmMaterialAttributeValue();
		value.setAttrId(Integer.valueOf(attrId));
		value.setAttrValueName(attrValueName);
		value.setCreateBy(createBy);
		jmMaterialAttributeValueMapper.insertSelective(value);
		return value.getAttrValueId();
	}
}
